package com.gec.hawaste.shiro;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import com.gec.hawaste.entity.SysResource;
import com.gec.hawaste.entity.SysRole;
import com.gec.hawaste.entity.SysUser;
import com.gec.hawaste.entity.SysUserDo;
import com.gec.hawaste.service.ISysResourceService;
import com.gec.hawaste.service.ISysRoleService;
import com.gec.hawaste.service.ISysUserService;
import com.gec.hawaste.utils.CopyUtil;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;


public class MyRealm extends AuthorizingRealm {
    @Autowired
    ISysUserService sysUserService;
    @Autowired
    ISysResourceService sysResourceService;
    @Autowired
    ISysRoleService sysRoleService;

    // 认证
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        // 获取账号
        String username = (String) token.getPrincipal(); // 获取主体信息
        System.out.println("token:" + token);
        System.out.println("用户名：" + username);
//
        SysUserDo loginUser = null;
        try {
            loginUser = CopyUtil.copy(sysUserService.findUserByUsername(username), SysUserDo.class);
            System.out.println("1:" + loginUser);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (loginUser == null) {
            // 报UnknownAccountException
            return null;
        }

        List<SysResource> resources = sysResourceService.finResourceByUid(loginUser.getId());
        loginUser.setResources(resources);
        System.out.println("2:" + loginUser);
        System.out.println("用户信息:" + loginUser);
        String password_db = loginUser.getPassword();
        String salt_db = loginUser.getUsername();

        SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(loginUser, password_db,
                ByteSource.Util.bytes(salt_db), "MyRealm");
        return info;
    }

    // 授权:查询数据库合法用户的角色与权限
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principal) {
        // 获取当前用户
        SysUser user = (SysUser) principal.getPrimaryPrincipal();
        List<SysResource> list = null;
        // 查出该用户的权限
        list = sysResourceService.finResourceByUid(user.getId());
        // 将权限存储到集合
        List<String> resources = new ArrayList<>();
        for (SysResource s : list) {
            resources.add(s.getPermissionStr());
        }
        System.out.println("权限：" + resources);

        List<SysRole> list2 = null;
        list2 = sysRoleService.findRoleByUid(user.getId());
        // 将权限存储到集合
        List<String> roles = new ArrayList<>();
        for (SysRole s : list2) {
            roles.add(s.getName());
        }
        System.out.println("角色：" + roles);

        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
        // 将集合放入权限对象中
        info.addStringPermissions(resources);
        // 将集合放入角色对象中
        info.addRoles(roles);
        return info;
    }
}